import XCTest
import JLCommonKit

class Tests: XCTestCase {
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    
    func testStringRemovingPrefix() {
        // This is an example of a functional test case.
        let str = "JLCommonKit"
        XCTAssertEqual("CommonKit", str.removingPrefix("JL"))
    }
    
    func testStringRemovingSuffix() {
        // This is an example of a functional test case.
        let str = "JLCommonKit"
        XCTAssertEqual("JLCommon", str.removingSuffix("Kit"))
    }
    
    func testStringSafeIndex() {
        // This is an example of a functional test case.
        let str = "JLCommonKit"
        XCTAssertEqual(Character("L"), str[safe: 1])
    }
    
    func testStringSafeIndexNil() {
        // This is an example of a functional test case.
        let str = "JLCommonKit"
        XCTAssertNil(str[safe: 100])
    }
    
    func testStringSafeRange() {
        // This is an example of a functional test case.
        let str = "JLCommonKit"
        XCTAssertEqual("LC", str[safe: 1..<3])
    }
    
    func testStringSafeRangeNil() {
        // This is an example of a functional test case.
        let str = "JLCommonKit"
        XCTAssertNil(str[safe: 1..<100])
    }
    
    func testStringSlicng() {
        // This is an example of a functional test case.
        let str = "JLCommonKit"
        XCTAssertEqual("LCom", str.slicing(from: 1, length: 4))
    }
    
    func testStringSlice() {
        // This is an example of a functional test case.
        var str = "JLCommonKit"
        str.slice(from: 1, length: 4)
        XCTAssertEqual("LCom", str)
    }
    
    func testDictMerge1() {
        // This is an example of a functional test case.
        let dict:[String: String] = ["key1":"value1"] + ["key2":"value2"]
        let result = [
            "key1":"value1",
            "key2":"value2"
        ]
        XCTAssertEqual(result, dict)
    }
    
    func testDictMerge2() {
        // This is an example of a functional test case.
        var dict:[String: String] = ["key1":"value1"]
        dict+=["key2":"value2"]
        let result = [
            "key1":"value1",
            "key2":"value2"
        ]
        XCTAssertEqual(result, dict)
    }
    

    func testOptionalUnwrapped1() {
        // This is an example of a functional test case.
        let foo: String? = nil
        XCTAssertEqual("bar", foo.unwrapped(or: "bar"))
    }
    
    func testOptionalUnwrapped2() {
        // This is an example of a functional test case.
        var foo: String? = nil
        foo = "foo"
        XCTAssertEqual("foo", foo.unwrapped(or: "bar"))
    }
    
    enum MyError: Error {
        case jl
    }
    
    func testOptionalUnwrappedError1() {
        // This is an example of a functional test case.
        let foo: String? = nil
        XCTAssertThrowsError(try foo.unwrapped(or: MyError.jl))
    }
    
    func testOptionalUnwrappedError2() {
        // This is an example of a functional test case.
        var foo: String? = nil
        foo = "foo"
        XCTAssertEqual("foo", foo.unwrapped(or: "bar"))
    }
    


